package game.ui;

import javax.swing.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;

public class LoginFrame extends JFrame implements MouseListener {
    //登录相关
    JTextField jTextFieldName = new JTextField();
    JPasswordField jTextFieldPassword = new JPasswordField();
    JTextField jTextFieldCaptcha = new JTextField();
    JButton jButtonLogin = new JButton();
    JButton jButtonRegister = new JButton();
    String captcha;
    //验证码显示
    JLabel code = new JLabel();


    /*static ArrayList<User> userList = new ArrayList<>();
    static {
        userList.add(new User("zhangsan", "12345"));
    }*/

    public LoginFrame() {
        initFrame();
        initView();
        this.setVisible(true);
    }

    public void initFrame() {
        this.setSize(488, 430);
        //设置界面标题
        this.setTitle("拼图游戏登录");
        //设置置顶
        this.setAlwaysOnTop(true);
        //设置界面在屏幕居中
        this.setLocationRelativeTo(null);
        //按下关闭时结束程序
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setLayout(null);
        //initView();

    }

    public void initView() {
        JLabel jLabelUserName = new JLabel(new ImageIcon("JigsawPuzzles\\image\\login\\用户名.png"));
        jLabelUserName.setBounds(116, 135, 51, 19);
        this.getContentPane().add(jLabelUserName);

        JLabel jLabelPassword = new JLabel(new ImageIcon("JigsawPuzzles\\image\\login\\密码.png"));
        jLabelPassword.setBounds(130, 195, 35, 18);
        this.getContentPane().add(jLabelPassword);

        JLabel jLabelCaptcha = new JLabel(new ImageIcon("JigsawPuzzles\\image\\login\\验证码.png"));
        jLabelCaptcha.setBounds(133, 256, 50, 30);
        this.getContentPane().add(jLabelCaptcha);

        jTextFieldName.setBounds(195, 134, 200, 30);
        this.getContentPane().add(jTextFieldName);

        jTextFieldPassword.setBounds(195, 195, 200, 30);
        this.getContentPane().add(jTextFieldPassword);

        jTextFieldCaptcha.setBounds(195, 256, 100, 30);
        this.getContentPane().add(jTextFieldCaptcha);

        //验证码显示
        captcha = CaptchaUtil.captcha();

        code.setText(captcha);
        code.setBounds(300, 256, 50, 30);
        this.getContentPane().add(code);

        //登录按钮
        jButtonLogin.setIcon(new ImageIcon("JigsawPuzzles\\image\\login\\登录按钮.png"));
        jButtonLogin.setBounds(123, 310, 128, 47);
        //去除按钮默认边框
        jButtonLogin.setBorderPainted(false);
        //去除按钮默认背景
        jButtonLogin.setContentAreaFilled(false);
        jButtonLogin.setVisible(true);
        this.getContentPane().add(jButtonLogin);

        //注册按钮
        jButtonRegister.setIcon(new ImageIcon("JigsawPuzzles\\image\\login\\注册按钮.png"));
        jButtonRegister.setBounds(256, 310, 128, 47);
        jButtonRegister.setBorderPainted(false);
        jButtonRegister.setContentAreaFilled(false);
        jButtonRegister.setVisible(true);
        this.getContentPane().add(jButtonRegister);

        //事件绑定
        jButtonLogin.addMouseListener(this);
        jButtonRegister.addMouseListener(this);
        code.addMouseListener(this);

        //背景图
        JLabel background = new JLabel(new ImageIcon("JigsawPuzzles\\image\\login\\background.png"));
        background.setBounds(0, 0, 470, 390);
        this.getContentPane().add(background);
    }



    @Override
    public void mouseClicked(MouseEvent e) {
        Object obj = e.getSource();
        User rightUser = new User();
        if (obj == jButtonLogin) {
            String userName = jTextFieldName.getText();
            String password = jTextFieldPassword.getText();
            String captchaUser = jTextFieldCaptcha.getText();
            rightUser.setUsername(userName);
            rightUser.setPassword(password);
            if (userName.length()==0 ||password.length()==0) {
                    DialogUtil.showDialog("用户名或密码为空");
            } else if (!captchaUser.equalsIgnoreCase(captcha)) {
                DialogUtil.showDialog("验证码错误");
            } else if (!userInfo(rightUser)) {
                DialogUtil.showDialog("用户名或密码错误！");
            }else {
                System.out.println("验证通过!");
                this.setVisible(false);
                new GameFrame();
            }
        }
        if (obj == code){
            System.out.println("更换验证码");
            captcha = CaptchaUtil.captcha();
            code.setText(captcha);
        }
        if (obj == jButtonRegister) {
            this.setVisible(false);
            new RegisterFrame();
        }
    }

    @Override
    public void mousePressed(MouseEvent e) {
        Object obj = e.getSource();
        if (obj ==  jButtonLogin) {
            jButtonLogin.setIcon(new ImageIcon("JigsawPuzzles\\image\\login\\登录按下.png"));
        } else if (obj == jButtonRegister) {
            jButtonRegister.setIcon(new ImageIcon("JigsawPuzzles\\image\\login\\注册按下.png"));
        }
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        Object obj = e.getSource();
        if (obj ==  jButtonLogin) {
            jButtonLogin.setIcon(new ImageIcon("JigsawPuzzles\\image\\login\\登录按钮.png"));

        } else if (obj == jButtonRegister) {
            jButtonRegister.setIcon(new ImageIcon("JigsawPuzzles\\image\\login\\注册按钮.png"));
        }
    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }

    //提示框
    /*public void showDialog(String content) {
        JDialog jd = new JDialog();
        jd.setTitle("拼图游戏提醒您：");
        jd.setSize(200, 150);
        JLabel jLabel = new JLabel(content);
        jd.getContentPane().add(jLabel);
        jd.setModal(true);
        jd.setLocationRelativeTo(null);
        jd.setAlwaysOnTop(true);
        //jd.setBounds(0, 0, 200, 150);
        jd.setVisible(true);
    }*/

    //判断是都存在用户
    public boolean userInfo(User user) {
      /*userArray.setUsername("zhangsan");
      userArray.setPassword("12345");
      UserList.addUserList(userArray);*/
      UserList.addUserList(new User("admin", "1234"));
      ArrayList<User> userList = UserList.userList();
      if (userList.size() == 0) {
          System.out.println("暂无信息");
          return false;
      }
        for (int i = 0; i < userList.size(); i++) {
            User right = userList.get(i);
            if (right.getUsername().equals(user.getUsername()) && right.getPassword().equals(user.getPassword())) {
                return true;
            }
        }
        return false;

    }
}
